﻿using System;


using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using yiCommerce.Core;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Core.Domain.ShoppingCarts;
using yiCommerce.Core.Helper;
using yiCommerce.Models.Products;
using yiCommerce.Service.Loggings;
using yiCommerce.Service.Pictures;
using yiCommerce.Service.Products;
using yiCommerce.Service.ShoppingCarts;

namespace yiCommerce.Controllers
{
    public  class AddToCartProductModel
    {
        public int ProductVariantId { get; set; }
        public int Quantity { get; set; }
        public IList<int> ProductVariantAttributeValueIds { get; set; }
    }

    public class ShoppingCartController : Controller
    {
        private IProductService _productService;
        private IShoppingCartService _shoppingCartService;
        private IWorkContext _workContext;
        private IActivityService _activityService;

        private IPictureService _pictureService;
        private IProductVariantAttributeFormatter formatter;

        public ShoppingCartController(IProductService productService, IShoppingCartService shoppingCartService, IWorkContext workContext, IActivityService activityService, IPictureService pictureService, IProductVariantAttributeFormatter formatter)
        {
            _productService = productService;
            _shoppingCartService = shoppingCartService;
            _workContext = workContext;
            _activityService = activityService;
            _pictureService = pictureService;
            this.formatter = formatter;
        }

        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            return RedirectToAction("Cart");
        }

        public ActionResult Cart()
        {

            var customerId = _workContext.CurrentCustomer.Id;
            var cart = _shoppingCartService.GetShoppingCart(customerId);

            var model = PrepareShoppingCartModel(cart);
            ViewBag.Model = model;
            return View(model);
        }

        private ShoppingCartModel PrepareShoppingCartModel(IList<ShoppingCartItem> cart)
        {
            var model = new ShoppingCartModel();

            foreach (var shoppingCartItem in cart)
            {
                var pv = _productService.GetProductVariantById(shoppingCartItem.ProductVariantId);


                var shoppingCartItemModel = new ShoppingCartItemModel()
                    {
                        AttributeInfo = formatter.ToFormattedString(shoppingCartItem.AttributesXml),
                        PictureUrl = _pictureService.GetPictureUrl(pv.PictureId, 100, false),
                        ProductVariantId = pv.Id,
                        ProductId = pv.Product.Id,
                        ProductName = string.IsNullOrEmpty(pv.Product.ProductName) ? pv.Product.ShortDescription : pv.Product.ProductName,
                        Quantity = shoppingCartItem.Quantity,
                        Sku = pv.Sku,
                        SubTotal = pv.Price*shoppingCartItem.Quantity,
                        UnitPrice = pv.Price,
                    };

                model.Items.Add(shoppingCartItemModel);

            }
            return model;
        }
    }

    public class ShoppingCartModel 
    {
        public IList<ShoppingCartItemModel> Items { get; set; }
        public ShoppingCartModel()
        {
            Items = new List<ShoppingCartItemModel>();
        }
    }

    public partial class ShoppingCartItemModel 
    {
        public ShoppingCartItemModel()
        {
           // Warnings = new List<string>();

        }
        public int Id { get; set; }
        public string Sku { get; set; }

        public string PictureUrl { get; set; }

        public int ProductId { get; set; }
        public int ProductVariantId { get; set; }

        public string ProductName { get; set; }

        public decimal UnitPrice { get; set; }
        public int Quantity { get; set; }

        public decimal SubTotal { get; set; }

        public string AttributeInfo { get; set; }


       // public IList<string> Warnings { get; set; }

    }
}
